﻿using System;
using System.Collections.Generic;
using System.Linq;
using ExpenseSystem.Common;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using ExpenseSystem.Lor;

namespace ExpenseSystem.Fether
{
    public class CustomerFetcher : NormalFetcher<CustomerModel>
    {
        public CustomerModel GetByCode( long site, string code, out string message)
        {
            IList<CustomerModel> result = List( site, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, Restrain.Eq("Code", code).And(Restrain.Eq("Avaliable", true)));
            if (result.Count > 0)
                return result.First();
            return null;
        }
        public IList<CustomerModel> ListByKeyword( long site, string keyword, out string message,out int rows)
        {
            Restrain restrain = Restrain.Or(
                    Restrain.Or(Restrain.Lk("Code", "%" + keyword + "%"), Restrain.Lk("Name", "%" + keyword + "%")),
                    Restrain.Or(Restrain.Lk("Address", "%" + keyword + "%"), Restrain.Lk("DeliveryAddress", "%" + keyword + "%")));
            IList<CustomerModel> result = List( site, OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows,restrain.And(Restrain.Eq("Avaliable", true)));
            return result;
        }
        public IList<CustomerModel> ListByRegion( long site, long region, out string message,out int rows)
        {
            IList<CustomerModel> result = List( site, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, Restrain.Eq("RegionId", region).And(Restrain.Eq("Avaliable", true)));
            return result;
        }
        public IList<CustomerModel> ListBySales(long site, long sales, out string message,out int rows)
        {
            IList<CustomerModel> result = List( site, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, Restrain.Eq("SalesId", sales).And(Restrain.Eq("Avaliable", true)));
            return result;
        }
        public IList<CustomerModel> ListBySalesPoint(long site, long salesPoint ,out string message, out int rows)
        {
            IList<CustomerModel> result = List(site, OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows,Restrain.Eq("SalesPointId", salesPoint).And(Restrain.Eq("Avaliable", true)));
            return result;
        }
        public override IList<CustomerModel> List(long site, IList<OrderBy> orderBy, Limit limit, out string message,out int rows, params Restrain[] restrains)
        {
            if (userProfile.SpCtl)
            {
                if (restrains == null)
                {
                    restrains = new Restrain[] { Restrain.Eq("SalesPointId", userProfile.SalesPoint) };
                }
                else
                {
                    IList<Restrain> restrainsSwap = new List<Restrain>();
                    foreach(Restrain r in restrains)
                    {
                        restrainsSwap.Add(r);
                    }
                    restrainsSwap.Add(Restrain.Eq("SalesPointId", userProfile.SalesPoint));
                    restrains = restrainsSwap.ToArray();
                }
            }
            return base.List(site, orderBy, limit, out message,out rows, restrains);
        }
        public override CustomerModel Get(long site, long id, out string message)
        {
            CustomerModel result = base.Get(site, id, out message);
            if (result == null)
                return null;
            if (userProfile.SpCtl)
            {
                if (result.SalesPointId != userProfile.SalesPoint)
                {
                    message = "不允许跨门店信息获取数据";
                    return null;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
        public override IList<CustomerModel> List(long site, int state, IList<OrderBy> orderBy, Limit limit, out string message, out int rows,params Restrain[] restrains)
        {
            if (userProfile.SpCtl)
            {
                if (restrains == null)
                {
                    restrains = new Restrain[] { Restrain.Eq("SalesPointId", userProfile.SalesPoint) };
                }
                else
                {
                    Restrain[] restrainsSwap = new Restrain[restrains.Length];
                    restrains.CopyTo(restrainsSwap, 0);
                    restrains = new Restrain[restrainsSwap.Length + 1];
                    restrainsSwap.CopyTo(restrains, 0);
                    restrains[restrainsSwap.Length - 1] = Restrain.Eq("SalesPointId", userProfile.SalesPoint);
                }
            }
            return base.List(site, state, orderBy, limit, out message, out rows ,restrains);
        }
    }
}